﻿using System;
using System.Text;
using masterview.Code;
using masterview.Code.db;

namespace masterview
{
    public partial class WebForm6 : System.Web.UI.Page
    {
        public ServerSideMessage ssm = ServerSideMessage.GetInstance();

        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = "";
            var sb = new StringBuilder();
            try
            {
                var db = new Database();

                var services = db.GetListOfServicesWithFullInfo();

                sb.Append("<br />");
                if (services != null || services.Count != 0)
                {
                    foreach (var service in services)
                    {
                        sb.Append("Service ID = " + service.ID);
                        sb.Append("<br />");
                        sb.Append("Service Name = " + service.Name);
                        sb.Append("<br />");
                        sb.Append("Service Database = " + service.Database);
                        sb.Append("<br />");
                        foreach (var param in service.ServiceParameters)
                        {
                            sb.Append("-- " + param.Key + " = " + param.Value);
                            sb.Append("<br />");
                        }
                        foreach (var instance in service.Instances)
                        {
                            sb.Append("-----------");
                            sb.Append("Instance ID = " + instance.ID);
                            sb.Append("<br />");
                            sb.Append("Instance Name = " + instance.Name);
                            sb.Append("<br />");
                            sb.Append("Instance Owner Name = " + instance.OwnerName);
                            sb.Append("<br />");
                            sb.Append("Instance Owner Email = " + instance.OwnerEmail);
                            sb.Append("<br />");
                            sb.Append("Instance URL = <a href='" + instance.Url + "'>" + instance.Url + "</a>");
                            sb.Append("<br />");
                            sb.Append("----------- Used instances");
                            sb.Append("<br />");
                            foreach (var usedInstance in instance.UsedInstances)
                            {
                                sb.Append(" -- Id = " + usedInstance.ID + " Name = " + usedInstance.Name);
                                sb.Append("<br />");
                                sb.Append(" -- Offices: ");
                                foreach (var officeCode in usedInstance.Offices)
                                {
                                    sb.Append(officeCode + ", ");
                                }
                                sb.Append("<br />");
                            }
                        }
                        sb.Append("---------------------------------------");
                    }
                }
                else
                {
                    sb.Append("services = null or services.count = 0");
                }
            }catch(Exception ex)
            {
                ssm.IsActive = true;
                ssm.Message = ex.Message;
                ssm.Title = "Database";
                ssm.NotifyType = ServerSideMessage.MessageType.Error;
            }
            Label1.Text = sb.ToString();
        }
    }
}